package com.centaline.androidsalesblog.widgets.dropdown;

import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.AppCompatTextView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.model.businissobject.DropBo;

import java.util.ArrayList;

/**
 * 下拉菜单-多选
 * Created by guilin on 16/1/13.
 */
public class DropCheckAdapter extends BaseAdapter {

    private ArrayList<DropBo> list;
    private SparseBooleanArray sparseArray;
    private boolean isCheckMode;
    private int selectPos = -1;

    public DropCheckAdapter() {
        list = new ArrayList<>();
        sparseArray = new SparseBooleanArray();
    }

    public void setData(ArrayList<DropBo> arrayList, SparseBooleanArray booleanSparseArray) {
        isCheckMode = true;
        selectPos = -1;
        list.clear();
        list.addAll(arrayList);
        sparseArray = booleanSparseArray;
        notifyDataSetChanged();
    }

    public void setData(ArrayList<DropBo> arrayList, int position) {
        isCheckMode = false;
        selectPos = position;
        list.clear();
        list.addAll(arrayList);
        notifyDataSetChanged();
    }

    public void select(int position) {
        sparseArray.put(position, !sparseArray.get(position, false));
        selectPos = -1;
        notifyDataSetChanged();
    }

    public void setSelectPos(int selectPos) {
        setSelectPos(selectPos, true);
    }

    public void setSelectPos(int selectPos, boolean notify) {
        this.selectPos = selectPos;
        if (notify)
            notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DropVH dropVH;
        if (convertView == null) {
            dropVH = new DropVH();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_drop_checkable, parent, false);
            dropVH.atv_name = (AppCompatTextView) convertView.findViewById(R.id.atv_name);
            dropVH.checkBox = (AppCompatCheckBox) convertView.findViewById(R.id.checkbox);
            convertView.setTag(dropVH);
        } else {
            dropVH = (DropVH) convertView.getTag();
        }
        dropVH.atv_name.setText(list.get(position).getText());
        if (isCheckMode) {
            final boolean selected = sparseArray.get(position, false);
            dropVH.atv_name.setTextColor(selected || position == selectPos ? 0xFFF13E06 : 0xFF333333);
            dropVH.checkBox.setVisibility(list.get(position).isSelected() ? View.GONE : View.VISIBLE);
            dropVH.checkBox.setChecked(selected);
        } else {
            dropVH.atv_name.setTextColor(position == selectPos ? 0xFFF13E06 : 0xFF333333);
            dropVH.checkBox.setVisibility(View.GONE);
        }
        return convertView;
    }

    class DropVH {
        public AppCompatTextView atv_name;
        public AppCompatCheckBox checkBox;
    }
}
